<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * PENGUIN FrameWork
 * @author hungtd <tdhungit@gmail.com> 0972014011
 * @copyright Tran Dinh Hung 2011
 * 
 * Controller admin_users
 * ...
 * 
 * @package PenguinFW
 * @subpackage users
 * @version 1.0.0
 * 
 * @property User $User
 */
 
class admin_users extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        
        $this->layout->set_layout('admin');
        
        $this->model_name = 'User';
        
        $this->lang->load('generate', config_item('language'));
        $this->lang->load('user', config_item('language'));
        
        $this->load->model('User');
    }        
    
    /**
     * Dashboard
     */
    public function dashboard()
    {
        // check permission
        $this->PG_ACL('r');
        
        $this->layout->set_title(lang('Dashboard'));
        
        $data = array();
        
        $this->parser->parse('admin_users/dashboard', $data);
    }
    
    /**
     * List
     */
    public function index($cfn_id = 0)
    {
        // check permission
        $this->PG_ACL('r');
        
        // set title
        $this->layout->set_title(lang('User manager'));
        
        // set javascript to view
        $this->layout->set_javascript(array(
            'jquery.ui.core.min.js',
            'jquery.ui.datepicker.min.js'
        ));
        
        // set css to view
        $this->layout->set_rel(array(
            'jquery.ui.base.css',            
            'jquery.ui.datepicker.css'
        ));
        
        // filter users
        // filter user status id
        $filter_user_status_id = $this->input->get('user_status_id');
        if ($filter_user_status_id)
        {
            $this->paginator['where']['user_status_id'] = $filter_user_status_id;
        }
        
        // filter created from date
        $filter_from_date = $this->input->get('from_date');
        if ($filter_from_date)
        {
            $this->paginator['where']['DATE(created) >='] = standar_date($filter_from_date);
        }
        
        // filter created end date
        $filter_to_date = $this->input->get('to_date');
        if ($filter_to_date)
        {
            $this->paginator['where']['DATE(created) <='] = standar_date($filter_to_date);
        }
        
        // filter username
        $filter_username = $this->input->get('username');
        if ($filter_username)
        {
            $this->paginator['where']['username'] = $filter_username;
        }
        
        // only show user not in recycle bin
        $this->paginator['where']['is_delete'] = 0;

        // get user
        $users = $this->pagination(5);
        
        // get user status id
        $user_statuses = $this->User->find('all', array(
            'select' => 'id, name',
            'from' => 'user_statuses',
            'limit' => 0
        ));

        // set data view
        $data = array(
            'list_views' => $users,                        
            'cfn_id' => $cfn_id,
            'user_statuses' => $user_statuses,
            
            'this_resource' => $this->router->class,
            'cf_names' => $this->getCustomFieldName(NULL, FALSE),
            'fields' => $this->getCustomField($this->session->userdata('user_id'), $cfn_id, NULL, FALSE),
            'link_edit' => array(
                'View' => 'users/admin_users/view/'
            ),
            'pagination_link' => $this->getPaginationLink('/users/admin_users/index/' . $cfn_id, 5)
        );
        
        $this->parser->parse($this->router->class . '/index', $data);
    }
    
    /**
     * VIEW User
     * 
     * @param int $user_id
     */
    public function view($user_id = 0)
    {
        // check permission
        $this->PG_ACL('r');
        
        // set title
        $this->layout->set_title(lang('View user'));
        
        // get user data from database
        $user = $this->User->find('first', array(
            'select' => 'users.*, genders.name as gender, user_roles.name as role, user_levels.name as level, user_types.name as user_type',
            'from' => 'users',
            'leftjoin' => array(
                'genders' => 'genders.id = users.gender_id',
                'user_roles' => 'user_roles.id = users.user_role_id',
                'user_levels' => 'user_levels.id = users.user_level_id',
                'user_types' => 'user_types.id = users.user_type_id'
            ),
            'where' => array('users.id' => $user_id)
        ));                
        
        // set data to view
        $data = array(
            'user' => $user
        );
        
        // parser
        $this->parser->parse($this->router->class . '/view', $data);
    }
    
    /**
     * ADD User
     */
    public function add()
    {
        // check permission
        $this->PG_ACL('w');
        
        // set title
        $this->layout->set_title(lang('Add user'));
    }

    /**
     * EDIT User
     * 
     * @param int $user_id 
     */    
    public function edit($user_id = 0)
    {
        // check permission
        $this->PG_ACL('e');
        
        // set title
        $this->layout->set_title(lang('Edit user'));
    }
    
    /**
     * DELETE User          
     */
    public function delete()
    {
        // check permission
        $this->PG_ACL('d');
        
        // update is_delete = 1
        $this->deleteOnListView();
    }
    
    /**
     * View RecycleBIN
     */
    public function recyclebin($cfn_id = 0)
    {
        // check permission
        $this->PG_ACL('d');
        
        // set title
        $this->layout->set_title(lang('User manager'));
        
        // set javascript to view
        $this->layout->set_javascript(array(
            'jquery.ui.core.min.js',
            'jquery.ui.datepicker.min.js'
        ));
        
        // set css to view
        $this->layout->set_rel(array(
            'jquery.ui.base.css',            
            'jquery.ui.datepicker.css'
        ));
        
        // filter users
        // filter user status id
        $filter_user_status_id = $this->input->get('user_status_id');
        if ($filter_user_status_id)
        {
            $this->paginator['where']['user_status_id'] = $filter_user_status_id;
        }
        
        // filter created from date
        $filter_from_date = $this->input->get('from_date');
        if ($filter_from_date)
        {
            $this->paginator['where']['DATE(created) >='] = standar_date($filter_from_date);
        }
        
        // filter created end date
        $filter_to_date = $this->input->get('to_date');
        if ($filter_to_date)
        {
            $this->paginator['where']['DATE(created) <='] = standar_date($filter_to_date);
        }
        
        // filter username
        $filter_username = $this->input->get('username');
        if ($filter_username)
        {
            $this->paginator['where']['username'] = $filter_username;
        }
        
        // only show user not in recycle bin
        $this->paginator['where']['is_delete'] = 1;

        // get user
        $users = $this->pagination(5);
        
        // get user status id
        $user_statuses = $this->User->find('all', array(
            'select' => 'id, name',
            'from' => 'user_statuses',
            'limit' => 0
        ));

        // set data view
        $data = array(
            'list_views' => $users,                        
            'cfn_id' => $cfn_id,
            'user_statuses' => $user_statuses,
            
            'this_resource' => $this->router->class,
            'cf_names' => $this->getCustomFieldName(NULL, FALSE),
            'fields' => $this->getCustomField($this->session->userdata('user_id'), $cfn_id, NULL, FALSE),
            'link_edit' => array(
                'View' => 'users/admin_users/view/'
            ),
            'pagination_link' => $this->getPaginationLink('/users/admin_users/index/' . $cfn_id, 5)
        );
        
        $this->parser->parse($this->router->class . '/recyclebin', $data);
    }
    
    /**
     * RESTORE from Recycle Bin
     */
    public function restore()
    {
        // check permission
        $this->PG_ACL('d');
        
        // update is_delete = 0
        $this->restoreOnListView();
    }
    
    /**
     * LOGIN by Admin
     */
    public function login()
    {
        
    }
}
                
?>